<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 17-12-7
 * Time: 下午3:43
 */

namespace common\components\tools;


use common\models\credit_public\SlArea;
use yii\helpers\ArrayHelper;

class City
{
    /**
     * 地区名转拼音
     * @param $name
     * @return string
     */
    public static function transNameToPinYin($name)
    {
        $c = mb_strlen($name, 'utf8');
        $arr = array();
        $py = \Yii::$app->pinyin;
        for ($i = 0; $i < $c; $i ++){
            $x = mb_substr($name, $i, 1, 'utf8');
            $x = $py->str2py($x, false);
            $x = ucfirst($x);
            $arr[] = $x;
        }
        $y = Tools::arr2str($arr, ' ');
        $name = $y;
        return $name;
    }

    /**
     * 地区字符串转数组
     * @param $area_info
     * @param array $cut 需要缩减['p'=>['type'=>['省'], 'detail'=>['台湾'], 'except'=>['河南']]]
     * @param int $max
     * @return array
     */
    public static function getAreaArrByArea($area_info, $cut = array(), $max = 3)
    {
        $init_cut = [
            'p' => ['type'=>[], 'detail'=>[], 'except'=>[]],
            'c' => ['type'=>[], 'detail'=>[], 'except'=>[]],
            'q' => ['type'=>[], 'detail'=>[], 'except'=>[]],
            'd' => ['type'=>[], 'detail'=>[], 'except'=>[]],
        ];
        $cut = ArrayHelper::merge($init_cut, $cut);
        if (is_array($area_info)){
            $p = $area_info['p'];
            $c = $area_info['c'];
            $q = $area_info['q'];
            $d = $area_info['d'];
        }else{
            $t = Tools::str2arr($area_info, ' ');
            $p = isset($t[0])?$t[0]:null;
            $c = isset($t[1])?$t[1]:null;
            $q = isset($t[2])?$t[2]:null;
            $d = isset($t[3])?$t[3]:null;
        }
        foreach ($cut as $k => $v) {
            $o = $$k;
            $can = true;
            foreach ($v['except'] as $k1 => $v1) {
                if (strpos($o, $v1) !== false) {
                    $can = $can && false;
                }
            }
            if ($can) {
                foreach ($v['detail'] as $k1 => $v1) {
                    if (strpos($o, $v1)) {
                        $o = $v1;
                    }
                }
                foreach ($v['type'] as $k1 => $v1) {
                    $o = str_replace($v1, '', $o);
                }
            }
            $$k = $o;
        }
        $arr = array(
            'p' => $p,
            'c' => $c,
            'q' => $q,
            'd' => $d,
        );
        $arr = array_slice($arr, 0, $max);
        $arr = array_filter($arr);
        return $arr;
    }

    /**
     * 由地区获取邮编
     * @param $area_info
     * @return string
     */
    public static function getZipCodeByArea($area_info)
    {
        $area_check = self::getAreaArrByArea($area_info,
            array(
                'p' => array(
                    'type' => array('市'),
                    'detail' => array('台湾'),
//                    'except' => array('浙江'),
                ),
            )
        );
        $area_check = '中国,'.Tools::arr2str($area_check);
//        $zipcode_row = M('Area', C('DB_PUBLIC_TABLE_PREFIX'), C('DB_PUBLIC'))->where(array('merger_name'=>$area_check))->find();
//        $zipcode_row = M('credit_public.Area', 'sl_')->where(array('merger_name'=>$area_check))->find();
        $zipcode_row = SlArea::findOne(['merger_name'=>$area_check]);
        if ($zipcode_row){
            if ($zipcode_row->zip_code){
                $zip_code = $zipcode_row->zip_code;
            }else{
                $zip_code = '000000';
            }
        }else{
            $zip_code = '000000';
        }
        return $zip_code;
    }

    /**
     * 由地区获取邮编
     * @param $area_info
     * @return string
     */
    public static function newGetZipCodeByArea($area_info)
    {
        $area = null;
        $area_arr = Tools::str2arr($area_info, ' ');
        if (count($area_arr)==1){
            $area = str_replace('市', '', $area_arr[0]);
        }else{
            $area = end($area_arr);
        }
        $zipcode_row = SlArea::findOne(['name'=>$area]);
        if ($zipcode_row){
            if ($zipcode_row->zip_code){
                $zip_code = $zipcode_row->zip_code;
            }else{
                $zip_code = '000000';
            }
        }else{
            $zip_code = '000000';
        }
        return $zip_code;
    }
}